package com.jiandian;

import com.jiandian.configration.OssConfig;
import com.jiandian.consts.BannerConsts;
import com.jiandian.entity.Banner;
import com.jiandian.entity.GameInfo;
import com.jiandian.service.BannerService;
import com.jiandian.service.GameInfoService;
import com.jiandian.service.OssService;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.COSObjectInputStream;
import com.qcloud.cos.model.COSObjectSummary;
import com.qcloud.cos.model.GetObjectRequest;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class a {
    @Autowired
    private OssConfig ossConfig;

    @Autowired
    private COSClient cosClient;

    @Autowired
    private OssService ossService;

    @Autowired
    private GameInfoService gameInfoService;

    @Autowired
    private BannerService bannerService;

    @Test
    public void testList() throws UnsupportedEncodingException {
        gameInfoService.deleteByMaxId(10);
    }

    public static String getStreamString(InputStream tInputStream) {

        if (tInputStream != null) {

            try {
                BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(tInputStream));
                StringBuffer tStringBuffer = new StringBuffer();
                String sTempOneLine = new String("");
                while ((sTempOneLine = tBufferedReader.readLine()) != null) {
                    tStringBuffer.append(sTempOneLine);
                }
                return tStringBuffer.toString();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    @Test
    public void getObject() {

        // bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
        String bucketName = ossConfig.getBucketName();

        // String key="%E5%A4%A9%E4%B9%A6%E5%A5%87%E8%B0%88/name.txt";
        // 这里不能把key编码
        String key = "天书奇谈/name.txt";

        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        COSObject cosObject = cosClient.getObject(getObjectRequest);
        COSObjectInputStream cosObjectInput = cosObject.getObjectContent();
        String streamString = getStreamString(cosObjectInput);
        System.out.println(streamString);
    }

    @Autowired
    // private GameInfoService gameInfoService;

    @Test
    public void getOneGame() {
        Banner byId = bannerService.findById(2);
        byId.setUrl("333");
        bannerService.update(byId);

    }

    @Test
    public void split() {
        String a = "1;2;3;4;";
        String[] split = a.split(";");
        System.out.println(split.length);
    }
}
